// 引入模块
import axios from 'axios'

// 定义基准路径
axios.defaults.baseURL = 'http://localhost:8888/api/private/v1'
// 请求拦截
// Add a request interceptor
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  // 获取本地token
  let mytoken = localStorage.getItem('mytoken')
  // 判断本地有无token值
  if (mytoken) {
    config.headers['Authorization'] = mytoken
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// 左侧权限列表数据获取
export const rolesData = () => axios.get('roles').then(res => res.data)
// 登录请求
export const postLogin = (obj) => axios.post('login', obj).then(res => res.data)
// 所有权限列表
export const rightsData = () => axios.get('rights/list').then(res => res.data)
// 左侧菜单权限
export const getAllRightList = () => axios.get('menus').then(res => res.data)
// 删除角色指定权限
export const deleteAuth = (roleId, rightId) => axios.delete(`roles/${roleId}/rights/${rightId}`).then(res => res.data)
// 添加角色
export const addInfo = obj => axios.post('roles', obj).then(res => res.data)
// 编辑角色
export const editsubmit = (id, obj) => axios.put(`roles/${id}`, obj).then(res => res.data)
// 删除角色
export const deleteRole = id => axios.delete(`roles/${id}`).then(res => res.data)
// 获取所有树状权限列表
export const Rolelist = tree => axios.get(`rights/${tree}`).then(res => res.data)

// 获取订单管理列表
export const getOrdersList = (obj) => axios.get('orders', { params: obj }).then(res => res.data)
// 分类参数相关接口
// 获取商品列表
export const getCategories = (obj) => axios.get('categories', { params: obj }).then(res => res.data)
// 获取参数列表
export const getAttributes = (id, sel) => axios.get(`/categories/${id}/attributes`, { params: { sel: sel } }).then(res => res.data)
// 添加参数
export const addAttributes = (id, name, sel, vals) => axios.post(`/categories/${id}/attributes`, { attr_name: name, attr_sel: sel, attr_vals: vals }).then(res => res.data)
// 删除参数
export const delAttributes = (id, attrId) => axios.delete(`/categories/${id}/attributes/${attrId}`).then(res => res.data)

// 获取用户模块数据
export const getUserData = (obj) => axios.get('users', { params: obj }).then(res => res.data)
// 增加用户
export const addUser = (obj) => axios.post('users', obj).then(res => res.data)
// 删除用户
export const delUser = (id) => axios.delete(`users/${id}`).then(res => res.data)
// 编辑用户
export const editUser = (obj) => axios.put(`users/${obj.id}`, obj).then(res => res.data)
// 切换用户状态
export const toggleUser = (uId, type) => axios.put(`users/${uId}/state/${type}`).then(res => res.data)
// 获取角色列表
export const getRole = () => axios.get('roles').then(res => res.data)
// 分配角色
export const roleUser = (id, rid) => axios.put(`users/${id}/role`, { rid }).then(res => res.data)
// 获取统计数据
export const getReports = (type) => axios.get(`reports/type/${type}`).then(res => res.data)

// 获取商品数据列表
export const getGoods = obj => axios.get('/goods', { params: obj }).then(res => res.data)

// 商品分类相关接口
// 根据·id查询分类
export const getCatebyid = (id) => axios.get(`/categories/${id}`).then(res => res.data)
// 根据·id删除分类
export const delCatebyid = (id) => axios.delete(`/categories/${id}`).then(res => res.data)
// 编辑提交分类
export const editCatebyid = (id, catname) => axios.put(`/categories/${id}`, { cat_name: catname }).then(res => res.data)
// 添加分类
export const addCatebyid = (obj) => axios.post('/categories', obj).then(res => res.data)
// 编辑提交商品
// export const ediGoods = obj => axios.put(`/goods/${obj.id}`, { goods_name: obj.goods_name ,})

// 角色授权
export const authEdit = (roleId, obj) => axios.post(`roles/${roleId}/rights`, { rids: obj }).then(res => res.data)
